class LoginController < ApplicationController
  
  before_filter:check_user  ,:only=>[:list,:edit]
  
  def index
    @user = User.new
  end

  def new
    @user = User.new
  end

  def del
    @user =User.find(params[:id])
    if @user.destroy
      redirect_to :action=>:list
    end
  end

  def login
    @user=User.new(params[:user])
    dbuser = User.find_by_name(@user.name)
    
  #  render :text=>dbuser.id+"   "+@user.name
    if dbuser
      flash[:notice] = "User login success"
      session[:user] = @user
      redirect_to :action=>:list
    else
      flash[:notice] = "invalid name or password"
      render :action=>:index
    end
  end

  def logout
    session[:user] =nil
    redirect_to :action=>:index
  end

  def list
    @user = User.find(:all)
    @logout = true if session[:user]
  end

  def create
    @user = User.new(params[:user])
  
    if  @user.save
      flash[:notice] = "User register success"
      session[:user] = @user
      redirect_to :action=>:list
    else
      flash[:notice] = "就不给你注册"
      render :action=>:index
    end
         
          
  end

  def edit
    @user = User.find(params[:id])

  end
  
  def  sidecheck
    @user = User.new(params[:user])
    if params[:commit]=='register'
      render :action=>:create
    elsif params[:commit] == 'login'
      render :action=>:login
    end
  end
  
 def update
   @user = User.find(params[:id])
       if @user.update_attributes(params[:user])
      flash[:notice] = 'update your information success'
      redirect_to :action=>:list
       else
         flash[:notice] = 'update error!'
         render :action =>:edit
    end
 end
  
  def paramstest
    #@user = User.find(params[:id])
        @user = User.find_by_name(params[:name])
    # @id
  end
  
end

